package com.kfgj.hkp.common.utils;

import com.kfgj.hkp.common.BaseEntity;
import com.kfgj.hkp.common.context.CurrentContext;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;

/**
 * Created by liwensihan on 2018/5/23.
 */
public class OperateFillUtils {

    public static BaseEntity fill(BaseEntity entity){

        //填充创建者
        if (StringUtils.isEmpty(entity.getCreateCode())){
            if (CurrentContext.getUserInfo()!=null){
                entity.setCreateCode(CurrentContext.getUserInfo().getUserCode());
                entity.setCreateName(CurrentContext.getUserInfo().getUserName());
            }
        }
        //填充信息创建者部门
        if(StringUtils.isEmpty(entity.getCreateDeptCode())){
            if (CurrentContext.getUserInfo()!=null){
                entity.setCreateDeptCode(CurrentContext.getUserInfo().getOwnerDeptCode());
                entity.setCreateDeptName(CurrentContext.getUserInfo().getOwnerDeptName());
            }
        }
        //填充企业帐套信息
        if(StringUtils.isEmpty(entity.getCpyCode())){
            if (CurrentContext.getUserInfo()!=null){
                entity.setCpyCode(CurrentContext.getUserInfo().getCpyCode());
                entity.setCpyName(CurrentContext.getUserInfo().getCpyName());
            }
        }
        //填充信息所属者
        if (StringUtils.isEmpty(entity.getOwnerCode())){
            if (CurrentContext.getUserInfo()!=null){
                entity.setOwnerCode(CurrentContext.getUserInfo().getUserCode());
                entity.setOwnerName(CurrentContext.getUserInfo().getUserName());
            }
        }
        //填充信息所属者所属部门
        if (StringUtils.isEmpty(entity.getOwnerDeptCode())){
            if (CurrentContext.getUserInfo()!=null){
                entity.setOwnerDeptCode(CurrentContext.getUserInfo().getOwnerDeptCode());
                entity.setOwnerDeptName(CurrentContext.getUserInfo().getOwnerDeptName());
            }
        }


        //填充最后更新者
        if(StringUtils.isEmpty(entity.getLastUpdateCode())){
            if (CurrentContext.getUserInfo()!=null){
                entity.setLastUpdateCode(CurrentContext.getUserInfo().getUserCode());
                entity.setLastUpdateName(CurrentContext.getUserInfo().getUserName());
            }
        }


        //补充日期
        Date dt = new Date();
        if (entity.getCreateTime()==null){
            entity.setCreateTime(dt);
        }

        if(entity.getLastUpdateTime()==null){
            entity.setLastUpdateTime(dt);

        }
        return entity;

    }
}
